package cinema;

import java.io.Serializable;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @authors Matt Couzens & Amanda Kosinski
 * @version 29 April 2012
 */

public class Movie implements Serializable {
	enum Detail { Title, Genre, Director, Length, Cast, ReleaseDate, Certificate, Language, Synopsis, Trailer; }
	Map<Detail, String> movieDetails = new EnumMap<Detail, String>(Detail.class);
	String[] detailsArray;

	public Movie() {
	}

	/*
	 * Constructs a movie
	 */
	
	/**
	 * @param title
	 * @param genre
	 * @param director
	 * @param length
	 * @param cast
	 * @param releaseDate
	 * @param certificate
	 * @param language
	 * @param synopsis
	 * @param trailer
	 */
	Movie(String title, 
			String genre, 
			String director, 
			String length, 
			String cast, 
			String releaseDate, 
			String certificate, 
			String language, 
			String synopsis, 
			String trailer){
		HashMap<Detail, String> m = new HashMap<Detail, String>();
		m.put(Detail.Title, title);
		m.put(Detail.Genre, genre);
		m.put(Detail.Director, director);
		m.put(Detail.Length, length);
		m.put(Detail.Cast, cast);
		m.put(Detail.ReleaseDate, releaseDate);
		m.put(Detail.Certificate, certificate);
		m.put(Detail.Language, language);
		m.put(Detail.Synopsis, synopsis);
		m.put(Detail.Trailer, trailer);
		this.movieDetails = m;
	}

	/**
	 * @return movie details
	 * an array of strings containing the movie details
	 */
	public String getMovieDetails(Detail detail) {
		return movieDetails.get(detail);
	}

	void sortMovieDetails(){
		detailsArray = new String[movieDetails.size()]; // TODO Copy to string array to enable sorting
		for(Detail d : movieDetails.keySet()) {
			detailsArray[d.ordinal()] = d.name() + ": " + movieDetails.get(d);
		}
	}

	/*
	 * Get's movie details
	 */
	public void getMovieDetails() {
		sortMovieDetails();
		for(String str : detailsArray){
			System.out.println(str);
		}
	}
	
	//Edits movie, details are listed and user types in number of detail to edit
	public void editMovie() {
		Scanner sc;
		int index = 1;
		Detail toEdit;
		Map<Integer, Detail> detailMap = new HashMap<Integer, Detail>();
		for(Detail detail : movieDetails.keySet()){
			String str = getMovieDetails(detail);
			System.out.println(index + ": " + detail + ": " + str);
			detailMap.put(index, detail);
			index++;
		}
		System.out.println("Type in the number of the detail you would like to edit:");
		sc = new Scanner(System.in);
		int selection = sc.nextInt();
		toEdit = detailMap.get(selection);
		System.out.println("You have chosen to edit: " + toEdit);
		System.out.println("Please type in new details:");
		sc = new Scanner(System.in);
		String str = sc.nextLine();
		System.out.println("Movie detail " + toEdit + " will be changed from '" + movieDetails.get(toEdit) + "' to '" + str + "'.");
		System.out.println("Are you sure? (y/n):");
		sc = new Scanner(System.in);
		String confirm = sc.next();
		if (confirm.equals("y")){
			movieDetails.put(toEdit, str);
			System.out.println("Detail " + toEdit + " has been changed.");
		}
		else System.out.println("No changes have been made.");
	}

}